package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.BusinesssellerAuthentication;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.Option;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.service.OptionService;
import com.sl.au.service.AccountService;
import com.sl.au.service.RoleService;
import com.sl.au.service.UserService;
import com.sl.au.service.BusinesssellerBaseInfoService;

@RestController
@RequestMapping(value = "/ui/userset") 

public class UserSetController extends BaseController{
	
	@Autowired
	UserService userService;
	
	@Autowired
	BusinesssellerBaseInfoService businesssellerBaseInfoService;
	
	@Autowired
	OptionService optionService;
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		
		ModelAndView modelAndView = this.createLayoutView("userSet/userSet");
		List<Option> areas =optionService.getRepository().findByGroup("区域范围");
		List<User> areaMpersons =userService.getRepository().findAll();
		
	
		modelAndView.addObject("registerOpenid", this.getMyAccount().getRegisterOpenid());
		modelAndView.addObject("user", this.getMyAccount().getUser());
		modelAndView.addObject("areas", areas);
		modelAndView.addObject("areaMpersons", areaMpersons);
		String codeAdress=this.businesssellerBaseInfoService.getRepository().getInfoByUserId(this.getMyAccount().getUser().getId()).getCodeAdress();
		modelAndView.addObject("codeAdress", codeAdress);
		return modelAndView;
	}
	
	@RequestMapping(value = "updatePwd", method = RequestMethod.POST)
	public Map<String, Object> updatePwd(@RequestBody Map<String, Object> list) {
		Map<String, Object> map = new HashMap<String, Object>();
        int code =-1;
        String str="未知错误";
        
        String opwd=(String)list.get("opwd");
        String npwd=(String)list.get("npwd");
        
        if(this.getMyAccount().getPassword().equals(opwd))
        {
        	Account account =this.getMyAccount();
        	account.setPassword(npwd);
        	this.accountService.getRepository().save(account);
        	code=1;
        	str="修改密码成功";
        }else
        {
        	code=-1;
        	str= "旧密码不正确";
        }
        
        map.put("message", str);
		map.put("code", code);
		return map;
	}
	@RequestMapping(value = "setWxCode", method = RequestMethod.POST)
	public Map<String, Object> setWxCode(@RequestBody Map<String, Object> list) {
		Map<String, Object> map = new HashMap<String, Object>();
        int code =-1;
        String wxCode =(String) list.get("wxCode");
        
        Account account =this.getMyAccount();
    	account.setRegisterOpenid(wxCode);
    	this.accountService.getRepository().save(account);
    	code=1;
    	
		map.put("code", code);
		return map;
	}
}
